/*
 * Copyright 2009 XueSong Guo.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.webwheel.database.engine;

import java.io.*;
import java.sql.SQLException;

public class Blob implements java.sql.Blob {

    private byte[] buf;

    public static Blob valueOf(java.sql.Blob blob) throws IOException, SQLException {
        if(blob==null) return null;
        return new Blob(blob);
    }

    public Blob(java.sql.Blob blob) throws SQLException, IOException {
        InputStream is = blob.getBinaryStream();
        ByteArrayOutputStream baos;
        try {
            baos = new ByteArrayOutputStream((int) blob.length());
        } catch (Exception e) {
            baos = new ByteArrayOutputStream();
        }
        byte[] buf = new byte[1024];
        int rd;
        while ((rd = is.read(buf)) != -1) {
            baos.write(buf, 0, rd);
        }
        is.close();
        this.buf = baos.toByteArray();
    }

    public long length() throws SQLException {
        return buf.length;
    }

    public byte[] getBytes(long pos, int length) throws SQLException {
        byte[] nb = new byte[length];
        System.arraycopy(buf, (int) pos, nb, 0, length);
        return nb;
    }

    public InputStream getBinaryStream() throws SQLException {
        return new ByteArrayInputStream(buf);
    }

    public long position(byte pattern[], long start) throws SQLException {
        throw new UnsupportedOperationException();
    }

    public long position(java.sql.Blob pattern, long start) throws SQLException {
        throw new UnsupportedOperationException();
    }

    public int setBytes(long pos, byte[] bytes) throws SQLException {
        throw new UnsupportedOperationException();
    }

    public int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException {
        throw new UnsupportedOperationException();
    }

    public OutputStream setBinaryStream(long pos) throws SQLException {
        throw new UnsupportedOperationException();
    }

    public void truncate(long len) throws SQLException {
        if (len < buf.length) {
            byte[] nb = new byte[(int) len];
            System.arraycopy(buf, 0, nb, 0, nb.length);
            buf = nb;
        }
    }

    public void free() throws SQLException {
        buf = null;
    }

    public InputStream getBinaryStream(long pos, long length) throws SQLException {
        return new ByteArrayInputStream(buf, (int)pos, (int)length);
    }
}
